package logobj.btd;

import logobj.config.ConfProperties;
import logobj.entity.bak.LogFileInFo;
import logobj.utils.BigFileUtils;
import logobj.utils.OutLog;
import logobj.utils.TimeUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class TextFileDispost {

    private List<LogFileInFo> createPrecisionSplitIndex(Map<String, ArrayList> fineinfomap){
        if(fineinfomap == null){
            BigFileUtils bigFileUtils = new BigFileUtils();
            fineinfomap = bigFileUtils.getFileInfo(ConfProperties.ftp_localpath);
        }
        ArrayList namelist = fineinfomap.get("namelist");
        ArrayList sizelist =fineinfomap.get("sizelist");
        int filenum = namelist.size();
        List<LogFileInFo> logFileInFoList = new ArrayList<LogFileInFo>();
        LogFileInFo logFileInFo = null;
        File ftpfile = null;
        BigFileUtils bigFileUtils=null;
        //建立文本文件索引
        for(int i = 0 ;i< filenum;i++){
            //如果大于最小文件大小，则建立文件索引
            if( (long)sizelist.get(i) > ConfProperties.min_file_size ){
                bigFileUtils = new BigFileUtils();
                logFileInFo = bigFileUtils.createIndex(ConfProperties.ftp_localpath+"/"+namelist.get(i),
                        ConfProperties.ftp_localpath_index+"/"+ String.valueOf(namelist.get(i)).replace(".log","")  +".ind");
                logFileInFoList.add(logFileInFo);
                OutLog.printf("完成对 "+logFileInFo.getFilename()+" 文件的字节索引建立-->"+ logFileInFo.getIndexname());
            }else {
                //移动文件到子文件夹
                ftpfile = new File(ConfProperties.ftp_localpath+"/"+namelist.get(i));
                ftpfile.renameTo(new File(ConfProperties.ftp_localpath_sublog+"/"+namelist.get(i)));
                OutLog.printf(namelist.get(i) +" 文件小于设置切割文件值大小，已移动文件到子文件夹！");
            }
            bigFileUtils =null;
            System.gc();
        }
        return logFileInFoList;
    }
    //进行分割
    public void fSplit(Map<String, ArrayList> fineinfomap){
        if("fast".equals(ConfProperties.file_incision_model)  ){
            fastSplit(fineinfomap);
        }else if ("precision".equals(ConfProperties.file_incision_model) ){
            precisionSplit(fineinfomap);
        }
    }

    //精确分割模式
    private void precisionSplit(Map<String, ArrayList> fineinfomap){
        if(fineinfomap == null){
            BigFileUtils bigFileUtils = new BigFileUtils();
            fineinfomap = bigFileUtils.getFileInfo(ConfProperties.ftp_localpath);
        }
        List<LogFileInFo> logFileInFoList = createPrecisionSplitIndex( fineinfomap);
        LogFileInFo logFileInFo = null;
        //进行切割
        BigFileUtils bigFileUtils = new BigFileUtils();
        for(int i = 0; i < logFileInFoList.size(); i++ ){
            logFileInFo = logFileInFoList.get(i);
            OutLog.printf("开始对"+logFileInFo.getFilename()+"进行日志切割.....");
            int line =  (int)logFileInFo.getFilerownum()/ConfProperties.file_incision_value;
            for(int c = 0 ; c < ConfProperties.file_incision_value ; c++){
                //根据行数获取字节
                long[] poit = bigFileUtils.getTextSizePoit( logFileInFo.getIndexname(),line * c ,line * (c+1));
                String sublog = ConfProperties.ftp_localpath_sublog+"/"
                        + logFileInFo.getFilename().substring(logFileInFo.getFilename().lastIndexOf("/")+1,logFileInFo.getFilename().length()-4)
                        +"_"+c+".log";
                bigFileUtils.fileFastCopy(logFileInFo.getFilename(),sublog , poit[0], poit[1] - poit[0] );
        }
            try {
                Thread.sleep(1000 * ConfProperties.file_incision_interval_time);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    //快速分割模式
    private void fastSplit(Map<String, ArrayList> fineinfomap){
        if(fineinfomap == null){
            BigFileUtils bigFileUtils = new BigFileUtils();
            fineinfomap = bigFileUtils.getFileInfo(ConfProperties.ftp_localpath);
        }
        ArrayList namelist = fineinfomap.get("namelist");
        ArrayList sizelist =fineinfomap.get("sizelist");
        File ftpfile = null;
        Number nb;
        int subnum ;
        BigFileUtils bigFileUtils = null;
        //进行切割
        for(int i = 0; i < namelist.size(); i++ ){
           // logFileInFo = logFileInFoList.get(i);
            System.out.println("["+ TimeUtils.getStringNowDate()+"]:开始对"+namelist.get(i)+"进行日志切割:");
            nb=(Number)sizelist.get(i);
            subnum = nb.intValue()/ConfProperties.file_load_max_mem + 1;
            int endbyte = nb.intValue()%ConfProperties.file_load_max_mem;
            int endsize = 0;
            if( subnum > 1 ){
                for(int c = 0 ; c < subnum ; c++){
                    bigFileUtils = new BigFileUtils();
                    //根据行数获取字节
                    String sublog = ConfProperties.ftp_localpath_sublog+"/"+ namelist.get(i)+"_"+c+".log";
                    if(c + 1 == subnum){
                        endsize = endbyte;
                    }else {
                        endsize = ConfProperties.file_load_max_mem;
                    }
                    bigFileUtils.fileFastCopy(ConfProperties.ftp_localpath+"/"+ namelist.get(i),sublog , c * ConfProperties.file_load_max_mem,endsize );
                    bigFileUtils = null;
                    System.gc();
                }
                try {
                    Thread.sleep(1000 * ConfProperties.file_incision_interval_time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }else {
                //移动文件到子文件夹
                ftpfile = new File(ConfProperties.ftp_localpath+"/"+namelist.get(i) );
                ftpfile.renameTo(new File(ConfProperties.ftp_localpath_sublog+"/"+namelist.get(i) ));
                System.out.println("["+ TimeUtils.getStringNowDate()+"]:"+namelist.get(i) +" 文件小于设置切割文件值大小，已移动文件到子文件夹！");
            }
        }

    }


}
